#include <iostream>
#include "d_node.h"
#include "card.h"

using namespace std;

class DeckOfCards
{
  friend ostream& operator<<(ostream &ostr, const DeckOfCards &deck);

 public:
  DeckOfCards();

 private:
  node<card> *firstNode;
};

DeckOfCards::DeckOfCards ()
//Start with the last node (ie assign next to NULL)
//Then build deck from top to bottom
{
  //Build all cards and keep track of previous node
  node<card> *prevNode;
  node<card> *currentNode;

  //prevNode starts as the last node in the linked list
  prevNode = NULL;

  //Make new card for each suit value (starting with Spades)
  for (int suit=4; suit>=1; suit--)
  {

    //Make new card for each face value (starting with Ace)
    for (int val=14; val>=2; val--)
    {
      card mycard(val, suit);
      //Point current node to prev node
      currentNode = new node<card>(mycard, prevNode);
      //Reset the prev node
      prevNode = currentNode;
    }

  }

  //Finally, assign the private data to the first node
  firstNode = currentNode;

}


ostream& operator<<(ostream& ostr, const DeckOfCards &deck)
//Overloaded print operator
{

  //Keep track of current node
  node<card> *currentNode;
  currentNode = deck.firstNode;

  //While inside the linked list
  while (currentNode != NULL)
  {
    ostr << currentNode->nodeValue << endl;
    //reset the current node
    currentNode = currentNode->next;
  }

  return ostr;
}
